﻿using UnityEngine;
using System.Collections;
using EnemyClasses;
using PlayerClasses;
using GeneralStorageClasses;
using TileMapClasses;

/// <summary>
/// Window loss. This is the class to determine whether the player has won or lost in the battlefield scene.
/// </summary>
public class WinLoss : MonoBehaviour
{
	private bool win;
	private bool lose;

	public GUIStyle victory;
	public GUIStyle loss;

	public void Start ()
	{
		PlayerController.PlayerDeathEvent += Lose;
		Boss.BossDeathEvent += Win;
		tileManager.Instance.Hide ();
	}

	public void OnDestroy ()
	{
		PlayerController.PlayerDeathEvent -= Lose;
		Boss.BossDeathEvent -= Win;
	}

	/// <summary>
	/// Win function called when the player has won.
	/// </summary>
	public void Win ()
	{
		win = true;
	}

	/// <summary>
	/// Lose function called when the player has lost.
	/// </summary>
	public void Lose ()
	{
		lose = true;
	}

	public void OnGUI ()
	{
		if (win) {
			GUI.Box (new Rect ((Screen.width - 1024) / 2, (Screen.height - 512) / 2, 1024, 512), "", victory);
			Time.timeScale = 0;
			GameManager.Instance.battleWin = 1;
			//tileManager.Instance.UnHide ();
			//Application.LoadLevel ("OverWorld");
		}

		if (lose) {
			GUI.Box (new Rect ((Screen.width - 1024) / 2, (Screen.height - 512) / 2, 1024, 512), "", loss);
			Invoke ("Die", 1);
			//tileManager.Instance.UnHide ();
			GameManager.Instance.battleWin = -1;
			//Application.LoadLevel ("OverWorld");	
		}
	}

	/// <summary>
	/// When the player has died the time scale gets set to 0.
	/// </summary>
	private void Die ()
	{
		Time.timeScale = 0;
	}
}
